﻿<?php

/**
 * Crc CMS
 *
 * @category   Crc
 * @package    Crc_Modules
 * @subpackage User
 * @author     Moldovan Calin <mcalin45@gmail.com>
 * @author     Vlad Razvan <razvanvlad1982@yahoo.com>
 * @copyright  Copyright (c) 2012
 * @version    $Id: AclRoles.php 39 2012-08-14 16:29:34Z razvanvlad1982@gmail.com $
 */

/**
 * Form for user
 *
 * @category   Crc
 * @package    Crc_Modules
 * @subpackage User
 * @author     Moldovan Calin <mcalin45@gmail.com>
 * @author     Vlad Razvan <razvanvlad1982@yahoo.com>
 * @copyright  Copyright (c) 2012
 */

class Modules_User_Forms_User extends Crc_Form
{
    /**
     * Sub forms
     *
     * @var array
     */
    public $subForms;

    /**
     * Constructor
     *
     * Registers options for the form
     *
     * @param array $options
     * @return void
     */
    public function __construct($options = array())
    {
        $this->subForms = array(
            'login'       => array('email', 'pass'),
            'changePass'  => array('pass', 'cpass'),
            'register'    => array('name', 'email', 'pass', 'cpass', 'address'),
            'recoverPass' => array('email'),
            'add'         => array('id_acl_role', 'name', 'email', 'pass', 'cpass', 'address'),
            'edit'        => array('id_acl_role', 'name', 'email', 'pass', 'address')
        );
        parent::__construct($options);
    }

    /**
     * Load form elements
     *
     * Registers options for the form
     *
     * @param string $subForm
     * @param string $formName
     * @return Zend_Form_SubForm
     */
    public function loadElements($subForm, $formName)
    {
        if (isset($this->subForms[$formName]) && is_array($this->subForms[$formName]) && !empty($this->subForms[$formName]))
        {
            foreach ($this->subForms[$formName] as $k => $v)
            {
                switch ($v)
                {
                    case 'id_acl_role':
                        $element = new Zend_Form_Element_Select($v);
                        $element->setLabel('Acl Role');
                        $roles = new Application_Model_Collection_AclRole();
                        $arrRoles = $roles->getRolesReccuring();
                        $element->addMultiOption('', 'Choose');
                        foreach ($arrRoles as $roleID => $role) {
                            $element->addMultiOption($roleID, $role['name']);
                        }
                        $element->setRequired(true);
                        $subForm->addElement($element);
                        break;

                    case 'name':
                        $element = new Zend_Form_Element_Text($v);
                        $element->setLabel('Name')
                                ->setRequired(true)
                                ->addValidator(new Zend_Validate_NotEmpty(), true)
                                ->addValidator(new Zend_Validate_Regex(array('pattern' => '~^[A-Za-z0-9-_ ]*$~')), true);
                        $subForm->addElement($element);
                        break;

                    case 'email':
                        $element = new Zend_Form_Element_Text($v);
                        $element->setLabel('E-mail')
                                ->setRequired(true)
                                ->addValidator(new Zend_Validate_NotEmpty(), true)
                                ->addValidator(new Zend_Validate_EmailAddress(), true);
                        if ($formName == 'recoverPass')
                        {
                            $element->addValidator(new Zend_Validate_Db_RecordExists(array(
                                'table'   => 'user',
                                'field'   => 'email',
                                'exclude' => 'status = 1'
                            )), true);
                        }

                        if ($formName == 'register' || $formName == 'add')
                        {
                            $element->addValidator(new Zend_Validate_Db_NoRecordExists(array(
                                'table' => 'user',
                                'field' => 'email'
                            )), true);
                        }

                        else if ($formName == 'edit')
                        {
                            $element->addValidator(new Zend_Validate_Db_NoRecordExists(array(
                                'table'   => 'user',
                                'field'   => 'email',
                                'exclude' => array('field' => 'id', 'value' => 0)
                            )), true);
                        }
                        $subForm->addElement($element);
                        break;

                    case 'pass':
                        $element = new Zend_Form_Element_Password($v);
                        $element->setLabel('Password');
                        if ($formName != 'edit')
                        {
                            $element->setRequired(true);
                        }
                        $element->addValidator(new Zend_Validate_StringLength(array('min' => 4)), true);
                        $subForm->addElement($element);
                        break;

                    case 'cpass':
                        $element = new Zend_Form_Element_Password($v);
                        $element->setLabel('Confirm password')
                                ->setRequired(true)
                                ->addValidator(new Zend_Validate_StringLength(array('min' => 4)), true)
                                ->addValidator(new Zend_Validate_Identical(array('token' => 'pass')), true);
                        $subForm->addElement($element);
                        break;

                    case 'address':
                        $element = new Zend_Form_Element_Textarea($v);
                        $element->setLabel('Address')
                                ->setRequired(true);
                        $subForm->addElement($element);
                        break;

                    default:
                        break;
                }
            }
        }
        $subForm->setOptions(array('legend' => $formName));
        return $subForm;
    }
}